home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / pearcmd.php < prev    next >
Encoding:
PHP Script  |  2005-12-02  |  14.3 KB  |  424 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@php.net>                                   |
  17. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  18. // |                                                                      |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: pearcmd.php,v 1.29 2005/11/12 02:26:53 cellog Exp $
  22.  
  23. ob_end_clean();
  24. if (!defined('PEAR_RUNTYPE')) {
  25.     // this is defined in peclcmd.php as 'pecl'
  26.     define('PEAR_RUNTYPE', 'pear');
  27. }
  28. define('PEAR_IGNORE_BACKTRACE', 1);
  29. if (!function_exists('file_get_contents')) {
  30.     function file_get_contents($filename)
  31.     {
  32.         $fp = fopen($filename, 'rb');
  33.         $ret = '';
  34.         while (!feof($fp)) {
  35.             $ret .= fread($fp, 8092);;
  36.         }
  37.         return $ret;
  38.     }
  39. }
  40. /**
  41.  * @nodep Gtk
  42.  */
  43. if ('\xampp\php\pear' != '@'.'include_path'.'@') {
  44.     ini_set('include_path', '\xampp\php\pear');
  45.     $raw = false;
  46. } else {
  47.     // this is a raw, uninstalled pear, either a cvs checkout, or php distro
  48.     $raw = true;
  49. }
  50. @ini_set('allow_url_fopen', true);
  51. if (!ini_get('safe_mode')) {
  52.     @set_time_limit(0);
  53. }
  54. ob_implicit_flush(true);
  55. @ini_set('track_errors', true);
  56. @ini_set('html_errors', false);
  57. @ini_set('magic_quotes_runtime', false);
  58. $_PEAR_PHPDIR = '#$%^&*';
  59. set_error_handler('error_handler');
  60.  
  61. $pear_package_version = "1.4.5";
  62.  
  63. require_once 'PEAR.php';
  64. require_once 'PEAR/Frontend.php';
  65. require_once 'PEAR/Config.php';
  66. require_once 'PEAR/Command.php';
  67. require_once 'Console/Getopt.php';
  68.  
  69.  
  70. PEAR_Command::setFrontendType('CLI');
  71. $all_commands = PEAR_Command::getCommands();
  72.  
  73. $argv = Console_Getopt::readPHPArgv();
  74. $progname = PEAR_RUNTYPE;
  75. if (in_array('getopt2', get_class_methods('Console_Getopt'))) {
  76.     array_shift($argv);
  77.     $options = Console_Getopt::getopt2($argv, "c:C:d:D:Gh?sSqu:vV");
  78. } else {
  79.     $options = Console_Getopt::getopt($argv, "c:C:d:D:Gh?sSqu:vV");
  80. }
  81. if (PEAR::isError($options)) {
  82.     usage($options);
  83. }
  84.  
  85. $opts = $options[0];
  86.  
  87. $fetype = 'CLI';
  88. if ($progname == 'gpear' || $progname == 'pear-gtk') {
  89.     $fetype = 'Gtk';
  90. } else {
  91.     foreach ($opts as $opt) {
  92.         if ($opt[0] == 'G') {
  93.             $fetype = 'Gtk';
  94.         }
  95.     }
  96. }
  97. $pear_user_config = '';
  98. $pear_system_config = '';
  99. $store_user_config = false;
  100. $store_system_config = false;
  101. $verbose = 1;
  102.  
  103. foreach ($opts as $opt) {
  104.     switch ($opt[0]) {
  105.         case 'c':
  106.             $pear_user_config = $opt[1];
  107.             break;
  108.         case 'C':
  109.             $pear_system_config = $opt[1];
  110.             break;
  111.     }
  112. }
  113.  
  114. PEAR_Command::setFrontendType($fetype);
  115. $ui = &PEAR_Command::getFrontendObject();
  116. $config = &PEAR_Config::singleton($pear_user_config, $pear_system_config);
  117.  
  118. if (PEAR::isError($config)) {
  119.     $_file = '';
  120.     if ($pear_user_config !== false) {
  121.        $_file .= $pear_user_config;
  122.     }
  123.     if ($pear_system_config !== false) {
  124.        $_file .= '/' . $pear_system_config;
  125.     }
  126.     if ($_file == '/') {
  127.         $_file = 'The default config file';
  128.     }
  129.     $config->getMessage();
  130.     $ui->outputData("ERROR: $_file is not a valid config file or is corrupted.");
  131.     // We stop, we have no idea where we are :)
  132.     exit();    
  133. }
  134.  
  135. // this is used in the error handler to retrieve a relative path
  136. $_PEAR_PHPDIR = $config->get('php_dir');
  137. $ui->setConfig($config);
  138. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
  139. if (ini_get('safe_mode')) {
  140.     $ui->outputData('WARNING: running in safe mode requires that all files created ' .
  141.         'be the same uid as the current script.  PHP reports this script is uid: ' .
  142.         @getmyuid() . ', and current user is: ' . @get_current_user());
  143. }
  144.  
  145. $verbose = $config->get("verbose");
  146. $cmdopts = array();
  147.  
  148. if ($raw) {
  149.     if (!$config->isDefinedLayer('user') && !$config->isDefinedLayer('system')) {
  150.         $found = false;
  151.         foreach ($opts as $opt) {
  152.             if ($opt[0] == 'd' || $opt[0] == 'D') {
  153.                 $found = true; // the user knows what they are doing, and are setting config values
  154.             }
  155.         }
  156.         if (!$found) {
  157.             // no prior runs, try to install PEAR
  158.             if (strpos(dirname(__FILE__), 'scripts')) {
  159.                 $packagexml = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'package2.xml';
  160.                 $pearbase = dirname(dirname(__FILE__));
  161.             } else {
  162.                 $packagexml = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'package2.xml';
  163.                 $pearbase = dirname(__FILE__);
  164.             }
  165.             if (file_exists($packagexml)) {
  166.                 $options[1] = array(
  167.                     'install',
  168.                     $packagexml
  169.                 );
  170.                 $config->set('php_dir', $pearbase . DIRECTORY_SEPARATOR . 'php');
  171.                 $config->set('data_dir', $pearbase . DIRECTORY_SEPARATOR . 'data');
  172.                 $config->set('doc_dir', $pearbase . DIRECTORY_SEPARATOR . 'docs');
  173.                 $config->set('test_dir', $pearbase . DIRECTORY_SEPARATOR . 'tests');
  174.                 $config->set('ext_dir', $pearbase . DIRECTORY_SEPARATOR . 'extensions');
  175.                 $config->set('bin_dir', $pearbase);
  176.                 $config->mergeConfigFile($pearbase . 'pear.ini', false);
  177.                 $config->store();
  178.                 $config->set('auto_discover', 1);
  179.             }
  180.         }
  181.     }
  182. }
  183. foreach ($opts as $opt) {
  184.     $param = !empty($opt[1]) ? $opt[1] : true;
  185.     switch ($opt[0]) {
  186.         case 'd':
  187.             list($key, $value) = explode('=', $param);
  188.             $config->set($key, $value, 'user');
  189.             break;
  190.         case 'D':
  191.             list($key, $value) = explode('=', $param);
  192.             $config->set($key, $value, 'system');
  193.             break;
  194.         case 's':
  195.             $store_user_config = true;
  196.             break;
  197.         case 'S':
  198.             $store_system_config = true;
  199.             break;
  200.         case 'u':
  201.             $config->remove($param, 'user');
  202.             break;
  203.         case 'v':
  204.             $config->set('verbose', $config->get('verbose') + 1);
  205.             break;
  206.         case 'q':
  207.             $config->set('verbose', $config->get('verbose') - 1);
  208.             break;
  209.         case 'V':
  210.             usage(null, 'version');
  211.         case 'c':
  212.         case 'C':
  213.             break;
  214.         default:
  215.             // all non pear params goes to the command
  216.             $cmdopts[$opt[0]] = $param;
  217.             break;
  218.     }
  219. }
  220.  
  221. if ($store_system_config) {
  222.     $config->store('system');
  223. }
  224.  
  225. if ($store_user_config) {
  226.     $config->store('user');
  227. }
  228.  
  229. $command = (isset($options[1][0])) ? $options[1][0] : null;
  230.  
  231. if (empty($command) && ($store_user_config || $store_system_config)) {
  232.     exit;
  233. }
  234.  
  235. if ($fetype == 'Gtk') {
  236.     if (!$config->validConfiguration()) {
  237.         PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
  238.             "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
  239.             'file to one of these locations, or use the -c and -s options to create one');
  240.     }
  241.     Gtk::main();
  242. } else do {
  243.     if ($command == 'help') {
  244.         usage(null, @$options[1][1]);
  245.     }
  246.     if (!$config->validConfiguration()) {
  247.         PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
  248.             "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
  249.             'file to one of these locations, or use the -c and -s options to create one');
  250.     }
  251.  
  252.     PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  253.     $cmd = PEAR_Command::factory($command, $config);
  254.     PEAR::popErrorHandling();
  255.     if (PEAR::isError($cmd)) {
  256.         usage(null, @$options[1][0]);
  257.     }
  258.  
  259.     $short_args = $long_args = null;
  260.     PEAR_Command::getGetoptArgs($command, $short_args, $long_args);
  261.     if (in_array('getopt2', get_class_methods('Console_Getopt'))) {
  262.         array_shift($options[1]);
  263.         $tmp = Console_Getopt::getopt2($options[1], $short_args, $long_args);
  264.     } else {
  265.         $tmp = Console_Getopt::getopt($options[1], $short_args, $long_args);
  266.     }
  267.     if (PEAR::isError($tmp)) {
  268.         break;
  269.     }
  270.     list($tmpopt, $params) = $tmp;
  271.     $opts = array();
  272.     foreach ($tmpopt as $foo => $tmp2) {
  273.         list($opt, $value) = $tmp2;
  274.         if ($value === null) {
  275.             $value = true; // options without args
  276.         }
  277.         if (strlen($opt) == 1) {
  278.             $cmdoptions = $cmd->getOptions($command);
  279.             foreach ($cmdoptions as $o => $d) {
  280.                 if (@$d['shortopt'] == $opt) {
  281.                     $opts[$o] = $value;
  282.                 }
  283.             }
  284.         } else {
  285.             if (substr($opt, 0, 2) == '--') {
  286.                 $opts[substr($opt, 2)] = $value;
  287.             }
  288.         }
  289.     }
  290.     $ok = $cmd->run($command, $opts, $params);
  291.     if ($ok === false) {
  292.         PEAR::raiseError("unknown command `$command'");
  293.     }
  294.     if (PEAR::isError($ok)) {
  295.         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
  296.         PEAR::raiseError($ok);
  297.     }
  298. } while (false);
  299.  
  300. // {{{ usage()
  301.  
  302. function usage($error = null, $helpsubject = null)
  303. {
  304.     global $progname, $all_commands;
  305.     $stderr = fopen('php://stderr', 'w');
  306.     if (PEAR::isError($error)) {
  307.         fputs($stderr, $error->getMessage() . "\n");
  308.     } elseif ($error !== null) {
  309.         fputs($stderr, "$error\n");
  310.     }
  311.     if ($helpsubject != null) {
  312.         $put = cmdHelp($helpsubject);
  313.     } else {
  314.         $put =
  315.             "Commands:\n";
  316.         $maxlen = max(array_map("strlen", $all_commands));
  317.         $formatstr = "%-{$maxlen}s  %s\n";
  318.         ksort($all_commands);
  319.         foreach ($all_commands as $cmd => $class) {
  320.             $put .= sprintf($formatstr, $cmd, PEAR_Command::getDescription($cmd));
  321.         }
  322.         $put .=
  323.             "Usage: $progname [options] command [command-options] <parameters>\n".
  324.             "Type \"$progname help options\" to list all options.\n".
  325.             "Type \"$progname help shortcuts\" to list all command shortcuts.\n".
  326.             "Type \"$progname help <command>\" to get the help for the specified command.";
  327.     }
  328.     fputs($stderr, "$put\n");
  329.     fclose($stderr);
  330.     exit;
  331. }
  332.  
  333. function cmdHelp($command)
  334. {
  335.     global $progname, $all_commands, $config;
  336.     if ($command == "options") {
  337.         return
  338.         "Options:\n".
  339.         "     -v         increase verbosity level (default 1)\n".
  340.         "     -q         be quiet, decrease verbosity level\n".
  341.         "     -c file    find user configuration in `file'\n".
  342.         "     -C file    find system configuration in `file'\n".
  343.         "     -d foo=bar set user config variable `foo' to `bar'\n".
  344.         "     -D foo=bar set system config variable `foo' to `bar'\n".
  345.         "     -G         start in graphical (Gtk) mode\n".
  346.         "     -s         store user configuration\n".
  347.         "     -S         store system configuration\n".
  348.         "     -u foo     unset `foo' in the user configuration\n".
  349.         "     -h, -?     display help/usage (this message)\n".
  350.         "     -V         version information\n";
  351.     } elseif ($command == "shortcuts") {
  352.         $sc = PEAR_Command::getShortcuts();
  353.         $ret = "Shortcuts:\n";
  354.         foreach ($sc as $s => $c) {
  355.             $ret .= sprintf("     %-8s %s\n", $s, $c);
  356.         }
  357.         return $ret;
  358.  
  359.     } elseif ($command == "version") {
  360.         return "PEAR Version: ".$GLOBALS['pear_package_version'].
  361.                "\nPHP Version: ".phpversion().
  362.                "\nZend Engine Version: ".zend_version().
  363.                "\nRunning on: ".php_uname();
  364.  
  365.     } elseif ($help = PEAR_Command::getHelp($command)) {
  366.         if (is_string($help)) {
  367.             return "$progname $command [options] $help\n";
  368.         }
  369.         if ($help[1] === null) {
  370.             return "$progname $command $help[0]";
  371.         } else {
  372.             return "$progname $command [options] $help[0]\n$help[1]";
  373.         }
  374.     }
  375.     return "Command '$command' is not valid, try 'pear help'";
  376. }
  377.  
  378. // }}}
  379.  
  380. function error_handler($errno, $errmsg, $file, $line, $vars) {
  381.     if ((defined('E_STRICT') && $errno & E_STRICT) || !error_reporting()) {
  382.         if (defined('E_STRICT') && $errno & E_STRICT) {
  383.             return; // E_STRICT
  384.         }
  385.         if ($GLOBALS['config']->get('verbose') < 4) {
  386.             return; // @silenced error, show all if debug is high enough
  387.         }
  388.     }
  389.     $errortype = array (
  390.         E_ERROR   =>  "Error",
  391.         E_WARNING   =>  "Warning",
  392.         E_PARSE   =>  "Parsing Error",
  393.         E_NOTICE   =>  "Notice",
  394.         E_CORE_ERROR  =>  "Core Error",
  395.         E_CORE_WARNING  =>  "Core Warning",
  396.         E_COMPILE_ERROR  =>  "Compile Error",
  397.         E_COMPILE_WARNING =>  "Compile Warning",
  398.         E_USER_ERROR =>  "User Error",
  399.         E_USER_WARNING =>  "User Warning",
  400.         E_USER_NOTICE =>  "User Notice"
  401.     );
  402.     $prefix = $errortype[$errno];
  403.     global $_PEAR_PHPDIR;
  404.     if (stristr($file, $_PEAR_PHPDIR)) {
  405.         $file = substr($file, strlen($_PEAR_PHPDIR) + 1);
  406.     } else {
  407.         $file = basename($file);
  408.     }
  409.     print "\n$prefix: $errmsg in $file on line $line\n";
  410. }
  411.  
  412.  
  413. /*
  414.  * Local variables:
  415.  * tab-width: 4
  416.  * c-basic-offset: 4
  417.  * indent-tabs-mode: nil
  418.  * mode: php
  419.  * End:
  420.  */
  421. // vim600:syn=php
  422.  
  423. ?>
  424.